package com.salestax.core.tests;

import java.math.BigDecimal;

import junit.framework.TestCase;

import com.salestax.core.ShoppingList;
import com.salestax.core.Product;
import com.salestax.core.Tax;
import com.salestax.core.specific.tax.ImportDuty;
import com.salestax.core.specific.tax.SalesTax;
import com.salestax.core.specific.tax.TaxRates;

public class TestCoreObject extends TestCase {

	private ShoppingList shoppingList = new ShoppingList();

	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testStart() {
		Product book = new Product("",12.49,1);
		Product musicCD = new Product("",14.99,1);
		musicCD.addTax(new SalesTax(TaxRates.SALES_TAX_RATE));
		Product chocolateBar = new Product("",0.85,1);
		shoppingList.addProduct(book).addProduct(musicCD).addProduct(chocolateBar);

		assertEquals("12.49", book.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("16.49", musicCD.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("0.85", chocolateBar.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());

		assertEquals("29.83", shoppingList.totalCost().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("1.50", shoppingList.totalTax().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	}

	public void testImported() {
		Product importedChocolates = new Product("",10.00,1);
		importedChocolates.addTax(new ImportDuty(TaxRates.IMPORT_DUTY));
		Product importedPerfumes = new Product("",47.50,1);
		importedPerfumes.addTax(new ImportDuty(TaxRates.IMPORT_DUTY));
		importedPerfumes.addTax(new SalesTax(TaxRates.SALES_TAX_RATE));
		shoppingList.addProduct(importedChocolates).addProduct(importedPerfumes);

		assertEquals("10.50", importedChocolates.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("54.65", importedPerfumes.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());

		assertEquals("65.15", shoppingList.totalCost().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("7.65", shoppingList.totalTax().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	}

	public void testImportedAndSalesTax() {
		Product importedPerfume = new Product("",27.99,1);
		importedPerfume.addTax(new ImportDuty(TaxRates.IMPORT_DUTY));
		importedPerfume.addTax(new SalesTax(TaxRates.SALES_TAX_RATE));
		Product perfume = new Product("",18.99,1);
		perfume.addTax(new SalesTax(TaxRates.SALES_TAX_RATE));
		Product hedachePill = new Product("",9.75,1);
		Product importedChocolate = new Product("",11.25,1);
		importedChocolate.addTax(new SalesTax(TaxRates.IMPORT_DUTY));
		shoppingList.addProduct(importedPerfume).addProduct(perfume).addProduct(
				hedachePill).addProduct(importedChocolate);

		assertEquals("32.19", importedPerfume.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("20.89", perfume.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("9.75", hedachePill.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("11.85", importedChocolate.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());

		assertEquals("6.70", shoppingList.totalTax().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
		assertEquals("74.68", shoppingList.totalCost().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
	}

}
